<?php
include_once("./function.php");
//print_r($_POST);die;//Array ( [title] => 音乐 [id] => 0 [author] => 刘德华 [keyword] => 民谣 [description] => 搜索 [text] => 一起音乐吧 [sub] => 添加 )
//print_r($_FILES);die;//Array ( [img] => Array ( [name] => Array ( [0] => 2.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\Windows\Temp\php420C.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 19041 ) ) )
extract($_POST);//$title $id $author $keyword $description $text
extract($_FILES);//$img
$title=trim($title);
$author=trim($author);
$description=trim($description);
//$text=trim(text);
//$title=trim(title);
//$img=implode(',',$img);
$time=trim(time());
function get_dwj($file)
{
	if(empty(implode("",$file["name"])))
	{
		return false;
	}
	$dir="./uploads/";
	if(empty($dir))
	{
		mkdir($dir,0777,true);
	}
		$res=array();$res1="";
		for($i=0;$i<count($file["name"]);$i++)
		{
			$num=$i+1;
		if(empty($file["name"][$i]))
		{
			$res1.="第{$num}个文件没有上传";
			continue;
		}

	$type=substr(strrchr($file["name"][$i],"."),1);
	$all=["jpg","jpeg","png","bmp","gif"];
	if(!in_array($type,$all))
	{
		$res1.=("第{$num}个文件类型不符合");
	}
	switch($file["error"][$i])
	{
		case 1:
			$err="超出服务器大小";
		break;
			case 2:
				$err="超出隐藏域设置的大小";
			break;
			case 3:
				$err="部分上传";
			break;
			case 4:
				$err="没有上传";
			break;
			case 6:
				$err="找不到临时文件夹";
			break;
			case 7:
				$err="写入临时文件夹出错";
			break;
	}
	if($file["error"][$i]!=0)
	{
		$res1.= $err;
		continue;
	}
	
	if(!is_uploaded_file($file["tmp_name"][$i]))
	{
		$res1.= "临时文件夹不存在";
		continue;
	}
	$path=$dir.date("His").rand(1000,9999).".".$type;
	if(move_uploaded_file($file["tmp_name"][$i],$path))
	{
		$res[]=$path;
	}
	else
	{
		$res1.= "第{$num}文件上传失败";	
	}
   }
	if($res1!="")
	{
		
		msg($res1,"product_add.php");exit;
	}
	else
	{
		return $res;
	}

}
$res=get_dwj($_FILES["img"]);
if($res!=false)
{
	$img=implode(",",$res);
}
else
{
	$img="";
}
$sql="insert into ts_news(news_cid,news_title,news_content,news_pubtime,news_author,news_descp,news_keyword,new_img,news_click) values('$id','$title','$text','$time','$author','$description','$keyword','$img','99')";
//echo $sql;die;
$res=mysqli_query($link,$sql);
//var_dump($res);die;
if($res)
{
	if(mysqli_affected_rows($link)==1)
	{
		msg("添加成功","./news.php");exit;
	}
	else
	{
		msg("添加失败","./news_add.php");exit;
	}

}
else
{
	echo "错误码:".mysqli_errno($link)."错误信息:".mysqli_error($link);exit;
}

?>